package com.dhee.creditcardsystem.dao.Impl;

import com.dhee.creditcardsystem.dao.AppDao;
import com.dhee.creditcardsystem.model.App;
import com.dhee.creditcardsystem.util.DBUtilAppC;
import com.dhee.creditcardsystem.util.DBUtilZSG;

import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class AppDaoImpl implements AppDao {
    // 数据库日期格式（需与实体类中字符串的格式一致，例如："yyyyMMddHH24miss"）
    private static final String DATE_FORMAT = "yyyyMMddHH24miss";

    public int insert(App app) {
        String sql = "INSERT INTO APP (" +
                // 表结构非空必填字段（移除APPCD，其余保留）
                "CSTID, SYOKBN, APPSTS, HNNFLG, KZKFLG, " +
                "INSERTDTE, INSERTID, UPDATEDTE, UPDATEID, DELETEFLG, " +
                // 你指定的11个字段
                "MAIL, BER, PHO, KJNHJN, SEIKJ, MEIKJ, SEIKN, MEIKN, SEIEN, MEIEN, SEX" +
                ") VALUES (" +
                "?, ?, ?, ?, ?, " +  // 非空字段占位符（移除APPCD对应的?）
                "TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), ?, " +  // INSERTDTE
                "TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), ?, ?, " +  // UPDATEDTE + 其他非空字段
                "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" +  // 11个指定字段占位符
                ")";

        // 准备参数（移除appcd对应的参数，顺序与SQL字段严格对应）
        Object[] params = {
                // 表结构非空必填字段（无APPCD）
                app.getCstid(),
                app.getSyokbn(),
                app.getAppsts(),
                app.getHnnflg(),
                app.getKzkflg(),
                app.getInsertdte(),
                app.getInsertid(),
                app.getUpdatedte(),
                app.getUpdateid(),
                app.getDeleteflg(),
                // 你指定的11个字段
                app.getMail(),
                app.getBer(),
                app.getPho(),
                app.getKjnhjn(),
                app.getSeikj(),
                app.getMeikj(),
                app.getSeikn(),
                app.getMeikn(),
                app.getSeien(),
                app.getMeien(),
                app.getSex()
        };

        try {
            // 直接执行插入，主键由数据库自增生成
            return DBUtilZSG.dml(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("插入数据失败", e);
        }
    }

    @Override
    public App selectByCstId(String cstid){
        String sql = "SELECT * FROM APP WHERE CSTID = ?";
        Object[] params = { cstid };
        App app = DBUtilAppC.dml(sql, params);
        return app;
    }



    @Override
    public int update(App app) {


        // 构建更新SQL，所有字段均支持更新，日期字段使用TO_TIMESTAMP转换
        String sql = "UPDATE APP SET " +

                "SYOKBN = ?, " +
                "MAIL = ?, " +
                "BER = ?, " +
                "PHO = ?, " +
                "UKTTME = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换
                "APPSTS = ?, " +
                "BRDCD = ?, " +
                "CRDSHUCD = ?, " +
                "KJNHJN = ?, " +
                "SEIKJ = ?, " +
                "SEIKN = ?, " +
                "SEIEN = ?, " +
                "MEIKJ = ?, " +
                "MEIKN = ?, " +
                "MEIEN = ?, " +
                "SEX = ?, " +
                "JKYSBT = ?, " +
                "TEL = ?, " +
                "POST = ?, " +
                "KNC = ?, " +
                "JSKJ1 = ?, " +
                "JSKJ2 = ?, " +
                "JSKN1 = ?, " +
                "JSKN2 = ?, " +
                "SPGTORKBN = ?, " +
                "SPGKBN = ?, " +
                "CSGKBN = ?, " +
                "TORKBN = ?, " +
                "DRIVERID = ?, " +
                "KKHCD = ?, " +
                "HGSUMK = ?, " +
                "KZKMLFLG = ?, " +
                "CAMMLFLG = ?, " +
                "BANKCD = ?, " +
                "BCHCD = ?, " +
                // 第二页字段
                "YKN = ?, " +
                "ACTCD = ?, " +
                "ACTNAE = ?, " +
                "ACTKKNDTE = ?, " +
                "ACTKKNID = ?, " +
                "ACTKKNNAM = ?, " +
                "AGTCD = ?, " +
                "HNNHHUCD = ?, " +
                "HNNFLG = ?, " +
                "HKS1 = ?, " +
                "HKS2 = ?, " +
                "HKS3 = ?, " +
                "HKS4 = ?, " +
                "HNNKKNDTE = ?, " +
                "HNNKKNID = ?, " +
                "HNNKKNNAM = ?, " +    //字段错误
                "GYOCD = ?, " +
                "KMS = ?, " +
                "KMSDEP = ?, " +
                "KMSTEL = ?, " +
                "KMSJS1 = ?, " +
                "KMSJS2 = ?, " +
                "NSHYM = ?, " +
                "NSG = ?, " +
                "KZKKBN = ?, " +
                "KZKFLG = ?, " +
                "KZKSEIKJ = ?, " +
                "KZKSEIKN = ?, " +
                "KZKSEIEN = ?, " +
                "KZKMEIKJ = ?, " +
                "KZKMEIKN = ?, " +
                "KZKMEIEN = ?, " +
                "KZKSEX = ?, " +
                "KZKGYOCD = ?, " +
                "KZKKMS = ?, " +
                "KZKKMSDEP = ?, " +
                "KZKKMSTEL = ?, " +
                "KZKHHUCD = ?, " +
                "KZKKS1 = ?, " +
                "KZKKS2 = ?, " +
                "KZKKS3 = ?, " +
                "KZKKS4 = ?, " +
                "KZKKKNDTE = ?, " +
                "KZKKKNID = ?, " +
                "KZKKKNNAM = ?, " +
                // 第三页字段
                "YSNINF = ?, " +
                "SKJSSKFLG = ?, " +
                "SSKIRITME = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换
                "SSKCD = ?, " +
                "SSKJRYTME = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换
                "APPONCTME = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换
                "OLDCRDCD = ?, " +
                "INSERTDTE = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换（非空）
                "INSERTID = ?, " +
                "UPDATEDTE = TO_TIMESTAMP(?, '" + DATE_FORMAT + "'), " +  // 日期转换（非空）
                "UPDATEID = ?, " +
                "DELETEFLG = ?, " +
                "DELETEDTE = TO_TIMESTAMP(?, '" + DATE_FORMAT + "') " +  // 日期转换
                // 主键条件（唯一更新）
                "WHERE cstid = ?";

        // 准备参数（顺序与SQL中SET后的字段顺序完全对应，最后追加主键条件）
        Object[] params = {
                // 第一页字段（除APPCD）

                app.getSyokbn(),
                app.getMail(),
                app.getBer(),
                app.getPho(),
                app.getUkttme(),  // 日期字符串
                app.getAppsts(),
                app.getBrdcd(),
                app.getCrdshucd(),
                app.getKjnhjn(),
                app.getSeikj(),
                app.getSeikn(),
                app.getSeien(),
                app.getMeikj(),
                app.getMeikn(),
                app.getMeien(),
                app.getSex(),
                app.getJkysbt(),
                app.getTel(),
                app.getPost(),
                app.getKnc(),
                app.getJskj1(),
                app.getJskj2(),
                app.getJskn1(),
                app.getJskn2(),
                app.getSpgtorkbn(),
                app.getSpgkbn(),
                app.getCsgkbn(),
                app.getTorkbn(),
                app.getDriverid(),
                app.getKkhcd(),
                app.getHgsumk(),
                app.getKzkmlflg(),
                app.getCammlflg(),
                app.getBankcd(),
                app.getBchcd(),
                // 第二页字段
                app.getYkn(),
                app.getActcd(),
                app.getActnae(),
                app.getActkkndte(),
                app.getActkknid(),
                app.getActkknnam(),
                app.getAgtcd(),
                app.getHnnhhucd(),
                app.getHnnflg(),
                app.getHks1(),
                app.getHks2(),
                app.getHks3(),
                app.getHks4(),
                app.getHnnkkndte(),
                app.getHnnkknid(),
                app.getHnnkknnam(),
                app.getGyocd(),
                app.getKms(),
                app.getKmsdep(),
                app.getKmstel(),
                app.getKmsjs1(),
                app.getKmsjs2(),
                app.getNshym(),
                app.getNsg(),
                app.getKzkkbn(),
                app.getKzkflg(),
                app.getKzkseikj(),
                app.getKzkseikn(),
                app.getKzkseien(),
                app.getKzkmeikj(),
                app.getKzkmeikn(),
                app.getKzkmeien(),
                app.getKzksex(),
                app.getKzkgyocd(),
                app.getKzkkms(),
                app.getKzkkmsdep(),
                app.getKzkkmstel(),
                app.getKzkhhucd(),
                app.getKzkks1(),
                app.getKzkks2(),
                app.getKzkks3(),
                app.getKzkks4(),
                app.getKzkkkndte(),
                app.getKzkkknid(),
                app.getKzkkknnam(),
                // 第三页字段
                app.getYsninf(),
                app.getSkjsskflg(),
                app.getSskiritme(),  // 日期字符串
                app.getSskcd(),
                app.getSskjrytme(),  // 日期字符串
                app.getApponctme(),  // 日期字符串
                app.getOldcrdcd(),
                app.getInsertdte(),  // 日期字符串（非空）
                app.getInsertid(),
                app.getUpdatedte(),  // 日期字符串（非空）
                app.getUpdateid(),
                app.getDeleteflg(),
                app.getDeletedte(),  // 日期字符串
                // 主键条件（最后一个参数）
                app.getCstid()
        };

        try {
            return DBUtilZSG.dml(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("更新APP表失败：APPCD=" + app.getAppcd(), e);
        }
    }
}
